﻿Namespace Buddies
    Friend Class Handler
        Private Shared mCache As New Generic.List(Of Entities.Buddy)
        Shared Sub New()
            Try
                SyncLock mCache
                    For Each clsBuddy As Entities.Buddy In DBController.GetAll
                        mCache.Add(clsBuddy)
                    Next
                End SyncLock
            Catch ex As Exception
                Framework.Misc.HandleException(ex)
            End Try
        End Sub

        Public Shared Function GetAll() As Generic.List(Of Entities.Buddy)
            SyncLock mCache
                Return mCache.ToList
            End SyncLock
        End Function

        Public Shared Function GetAllByIds(ByVal ids As Generic.List(Of Integer)) As Generic.List(Of Entities.Buddy)
            Dim aIds As New HashSet(Of Integer)
            For Each i As Integer In ids
                aIds.Add(i)
            Next
            SyncLock mCache
                Return (From b In mCache Where (aIds.Contains(b.Id)) Select b).ToList
            End SyncLock
        End Function
        Public Shared Function GetById(ByVal id As Integer) As Entities.Buddy
            SyncLock mCache
                Return (From b In mCache Where (b.Id = id) Select b).FirstOrDefault
            End SyncLock
        End Function
        Public Shared Function GetByUniqueId(ByVal uniqueId As String) As Entities.Buddy
            SyncLock mCache
                Return (From b In mCache Where (b.UniqueId = uniqueId) Select b).FirstOrDefault
            End SyncLock
        End Function

        Public Shared Sub Save(ByVal buddy As Entities.Buddy)
            Using t As New Data.TransactionScope
                Save(buddy, t)
                t.Complete()
            End Using
        End Sub
        Public Shared Sub Save(ByVal buddy As Entities.Buddy, ByVal transactionScope As Data.TransactionScope)
            Dim blnIsNew As Boolean = buddy.Id = 0
            Buddies.DBController.Save(buddy, transactionScope)
            If blnIsNew Then
                mCache.Add(buddy)
                Dim clsG As Entities.SecurityGroup = Security.Handler.GetByTypeAndSubType(Entities.SecurityGroupType.System, Entities.SecurityGroupSubType.AuthenticatedUsers)
                Security.Handler.AddBuddyToGroup(buddy.Id, clsG.Id, transactionScope)
            End If
            Manager.OnUpdated(buddy)
        End Sub
        Public Shared Sub Delete(ByVal buddy As Entities.Buddy)
            Using t As New Data.TransactionScope(Data.TransactionScope.TransactionMode.NullTransaction)
                Delete(buddy, t)
                t.Complete()
            End Using
        End Sub
        Public Shared Sub Delete(ByVal buddy As Entities.Buddy, ByVal transactionScope As Data.TransactionScope)
            DBController.Delete(buddy.Id, transactionScope)
            mCache.Remove(buddy)
            Manager.OnDeleted(buddy)
        End Sub
    End Class
End Namespace